//
//  MPDocumentHttpClient.m
//  SearchPeople
//
//  Created by zk on 16/5/10.
//
//

#import "MPDocumentHttpClient.h"
#import <AFNetworking.h>
#import "AFNetworkActivityIndicatorManager.h"

@implementation MPDocumentHttpClient

static MPDocumentHttpClient *g_instance = nil;

+ (MPDocumentHttpClient *)shareInstance{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        g_instance = [[MPDocumentHttpClient alloc] init];
        [g_instance initialize];
    });
    return g_instance;
}

- (void)initialize{
    //数据初始化
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
}

#pragma mark -

//上传图片
- (MPRequest *)uploadRequestWithName:(NSString *)requestName
                            withPath:(NSString *)path
                           withImage:(UIImage *)uploadImage{
    
    MPRequest *request = [[MPRequest alloc] init];
    
    request.requestName = [NSString stringWithFormat:@"%@",requestName];
    request.requestPath = [NSString stringWithFormat:@"%@",path];
    
    if (uploadImage == nil) {
        
        request.urlRequest = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            
        } error:nil];
    } else {
        request.urlRequest = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            NSData *imageData = UIImageJPEGRepresentation(uploadImage, 0.1);
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
            //后台 把上传图片的name指定了file了_(:з」∠)_
            [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
        } error:nil];
    }
    
    request.urlRequest.timeoutInterval = TimeOut;
    
    return request;
    
}

//普通请求
- (MPRequest *)requestWithName:(NSString *)requestName WithPath:(NSString *)path WithParameters:(NSDictionary *)parameters WithIsPost:(BOOL)isPost{
    
    MPRequest *request = [[MPRequest alloc] init];
    
    request.requestName = [NSString stringWithFormat:@"%@",requestName];
    request.requestPath = [NSString stringWithFormat:@"%@",path];
    [request.params addEntriesFromDictionary:parameters];
    
    if (isPost) {
        request.urlRequest = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:path parameters:parameters error:nil];
    } else {
        request.urlRequest = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:path parameters:parameters error:nil];
    }
    
    request.urlRequest.timeoutInterval = TimeOut;
    return request;
}

//上传图片
- (MPRequest *)uploadImage:(UIImage *)image
          WithSuccessBlock:(MPRequestCompletedBlock )successBlock
           WithFailedBlock:(MPRequestCompletedBlock )failedBlcok
{
    NSString *path = [NSString stringWithFormat:@"%@wechat/user/upload",HttpUrl];
    MPRequest *request = [self uploadRequestWithName:@"上传图片" withPath:path withImage:image];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlcok];
    return request;
}

//创建档案请求
- (MPRequest *)CreateDocumentWithType:(NSInteger)type WithpicUrl:(NSString *)picUrl WithName:(NSString *)name WithDescription:(NSString *)description WithTime:(NSString *)time WithMobile:(NSString *)mobile WithAddress:(NSString *)address WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlcok{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:[NSNumber numberWithInteger:type] forKey:@"type"];
    [params setObject:picUrl forKey:@"picUrl"];
    [params setObject:name forKey:@"name"];
    [params setObject:description forKey:@"description"];
    [params setObject:time forKey:@"time"];
    [params setObject:mobile forKey:@"mobile"];
    [params setObject:address forKey:@"address"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/foundperson/cMissPerson",HttpUrl];
    MPRequest *request = [self requestWithName:@"创建档案请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlcok];
    return request;
    
}

//删除档案请求
- (MPRequest *)DeleteDocumentWithID:(NSString *)ID WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:ID forKey:@"id"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/foundperson/dMissPerson",HttpUrl];
    MPRequest *request = [self requestWithName:@"删除档案请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//获取档案请求
- (MPRequest *)GetDocumentWithType:(NSInteger)type WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[NSNumber numberWithInteger:type] forKey:@"type"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/foundperson/rMissPerson",HttpUrl];
    MPRequest *request = [self requestWithName:@"获取档案请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//注册请求
- (MPRequest *)RegisterWithUsername:(NSString *)username WithPassword:(NSString *)Password WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlcok:(MPRequestCompletedBlock)failedBlcok{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:username forKey:@"username"];
    [params setObject:Password forKey:@"password"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/user/register",HttpUrl];
    MPRequest *request = [self requestWithName:@"注册请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlcok];
    return request;
}

//登录请求
- (MPRequest *)LoginWithUsername:(NSString *)username WithPassword:(NSString *)password WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:username forKey:@"username"];
    [params setObject:password forKey:@"password"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/user/login",HttpUrl];
    MPRequest *request = [self requestWithName:@"注册请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//修改用户信息

- (MPRequest *)ModifyUserInfoWithUserID:(NSString *)UserID WithHeadImage:(NSString *)headImage WithBirthday:(NSString *)birthday WithOcuppytion:(NSString *)ocuppytion WithBirthPlace:(NSString *)birthPlace WithLivePlace:(NSString *)livePlace WithQQ:(NSString *)qq WithMobile:(NSString *)mobile WithWechat:(NSString *)wechat WithGender:(NSString *)gender WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock )failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:UserID ? UserID : @"" forKey:@"user_id"];
    [params setObject:headImage ? headImage : @"" forKey:@"headImage"];
    [params setObject:birthday ? birthday : @"" forKey:@"birthday"];
    [params setObject:ocuppytion ? ocuppytion : @"" forKey:@"ocuppytion"];
    [params setObject:birthPlace ? birthPlace : @"" forKey:@"birthPlace"];
    [params setObject:livePlace ? livePlace : @"" forKey:@"livePlace"];
    [params setObject:qq ? qq : @"" forKey:@"qq"];
    [params setObject:mobile ? mobile : @"" forKey:@"mobile"];
    [params setObject:wechat ? wechat : @"" forKey:@"wechat"];
    [params setObject:gender ? gender : @"" forKey:@"gender"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/user/uUser",HttpUrl];
    MPRequest *request = [self requestWithName:@"修改用户信息请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//封禁用户
- (MPRequest *)BanUserWithUserID:(NSString *)UserID WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:UserID forKey:@"user_id"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/user/dUser",HttpUrl];
    MPRequest *request = [self requestWithName:@"封禁用户请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//评论
- (MPRequest *)CommentWithUserID:(NSString *)UserID WithContent:(NSString *)content WithMPID:(NSString *)mpid WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:UserID forKey:@"user_id"];
    [params setObject:content forKey:@"content"];
    [params setObject:mpid forKey:@"mp_id"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/comment/Comment",HttpUrl];
    MPRequest *request = [self requestWithName:@"评论请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//获得评论
- (MPRequest *)GetCommentWithMPID:(NSString *)mpid WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:mpid forKey:@"mp_id"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/comment/getComment",HttpUrl];
    MPRequest *request = [self requestWithName:@"获得评论请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//获得收藏列表
- (MPRequest *)GetCollectWithUserID:(NSString *)UserID WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:UserID forKey:@"user_id"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/attention/getAttention",HttpUrl];
    MPRequest *request = [self requestWithName:@"获得收藏列表请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

//添加至收藏列表

- (MPRequest *)AttentionWithMPID:(NSString *)MPID WithUserID:(NSString *)UserID WithSuccessBlock:(MPRequestCompletedBlock)successBlock WithFailedBlock:(MPRequestCompletedBlock)failedBlock{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:UserID forKey:@"user_id"];
    [params setObject:MPID forKey:@"mp_id"];
    
    NSString *path = [NSString stringWithFormat:@"%@wechat/attention/attention",HttpUrl];
    MPRequest *request = [self requestWithName:@"添加收藏请求" WithPath:path WithParameters:params WithIsPost:YES];
    [request startRequestWithSuccessBlock:successBlock WithFailedBlock:failedBlock];
    return request;
}

@end
